package com.ec50n9.server.module.ebookCategory

import com.ec50n9.server.module.common.Render
import com.ec50n9.server.module.common.Rendered
import org.hibernate.annotations.GenericGenerator
import org.springframework.beans.BeanUtils
import javax.persistence.*
import javax.validation.constraints.NotBlank

@Entity
class EbookCategory(
    @Column(unique = true)
    var name: String,

    @Column(unique = true)
    var code: String,

    @OneToMany
    var childrens: List<EbookCategory>? = null,

    @Id
    @GenericGenerator(name = "nanoid", strategy = "com.ec50n9.server.common.NanoIdGenerator")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "nanoid")
    var id: String? = null
) : Render<EbookCategory>()

/**
 * 查询渲染
 */
data class EbookCategoryQueryRendered(
    var name: String? = null,
    var code: String? = null
) : Rendered<EbookCategory> {
    override fun renderFrom(from: EbookCategory) {
        BeanUtils.copyProperties(from, this)
    }
}

/**
 * 创建表单
 */
data class EbookCategoryCreateForm(
    @field:NotBlank(message = "分类名不能为空")
    var name: String = "",

    @field:NotBlank(message = "分类代码不能为空")
    var code: String = ""
)

/**
 * 更新表单
 */
data class EbookCategoryUpdateForm(
    var name: String? = null,
    var code: String? = null
)
